package com.jcoinexchange.web.config;

import com.jcoinexchange.web.handler.GlobalExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.DispatcherServlet;

@AutoConfiguration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)  // 仅 Web 应用生效
@AutoConfigureAfter(WebMvcAutoConfiguration.class) // 确保 MVC 配置完成
public class GlobalExceptionHandlerAutoConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandlerAutoConfiguration.class);
    @Bean
    @ConditionalOnMissingBean  // 用户未自定义时生效
    @ConditionalOnClass(DispatcherServlet.class)  // 存在 Spring MVC 时生效
    public GlobalExceptionHandler globalExceptionHandler() {
        logger.info("start auto configuration globalExceptionHandler");
        return new GlobalExceptionHandler();
    }
}
